home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue30 / hash / HashArt1 / TSTHSHT1.DPR < prev   
Encoding:
Text File  |  1997-11-17  |  2.6 KB  |  84 lines

  1. program TstHshT1;
  2.  
  3. uses
  4.   SysUtils,
  5.   HashTbl1 in 'HashTbl1.pas';
  6.  
  7. const
  8.   ElementCount = 53;
  9.  
  10. function CalcELFHash(const S : string) : integer;
  11. var
  12.   G : longint;
  13.   i : integer;
  14. begin
  15.   Result := 0;
  16.   for i := 1 to length(S) do begin
  17.     Result := (Result shl 4) + ord(S[i]);
  18.     G := Result and $F0000000;
  19.     if (G <> 0) then
  20.       Result := Result xor (G shr 24);
  21.     Result := Result and (not G);
  22.   end;
  23. end;
  24.  
  25. const
  26.   TPEmployees : array [0..32] of string[15] =
  27.     ('Antypas',          'Bolduc',           'Brady',
  28.      'Bucknall',         'Cappellucci',      'Cope',
  29.      'DelRossi',         'Douglas',          'Fairweather',
  30.      'Foley',            'Frerking',         'Geiger',
  31.      'Ghinaudo',         'Horaz',            'Huffman',
  32.      'Hughes',           'Inman',            'Kokkonen',
  33.      'Larsen',           'Leier',            'Medlin',
  34.      'Milner',           'Phillips',         'Reisdorph',
  35.      'Roberts',          'Rogers',           'Rose',
  36.      'Salisbury',        'Trickey',          'Troxell',
  37.      'Turner',           'Warner',           'Welch');
  38.  
  39. var
  40.   i : integer;
  41.   HashTable : ThtHashTableLinear;
  42.  
  43. begin
  44.   HashTable := ThtHashTableLinear.Create(ElementCount, CalcELFHash);
  45.   try
  46.     {add all employee names}
  47.     for i := 0 to 32 do
  48.       HashTable.Insert(TPEmployees[i], nil);
  49.     HashTable.debugPrint('tstHash1.LOG', true);
  50.     {delete the first nine employee names}
  51.     for i := 0 to 9 do
  52.       HashTable.Delete(TPEmployees[i]);
  53.     HashTable.debugPrint('tstHash2.LOG', true);
  54.     {add the first nine employee names back in reverse order}
  55.     for i := 9 downto 0 do
  56.       HashTable.Insert(TPEmployees[i], nil);
  57.     HashTable.debugPrint('tstHash3.LOG', true);
  58.     {delete the odd-numbered employee names}
  59.     for i := 0 to 32 do
  60.       if Odd(i) then
  61.         HashTable.Delete(TPEmployees[i]);
  62.     HashTable.debugPrint('tstHash4.LOG', false);
  63.     {add the odd-numbered employee names >= 15 back}
  64.     for i := 15 to 32 do
  65.       if Odd(i) then
  66.         HashTable.Insert(TPEmployees[i], nil);
  67.     HashTable.debugPrint('tstHash5.LOG', false);
  68.     {add the odd-numbered employee names < 15 back}
  69.     for i := 0 to 14 do
  70.       if Odd(i) then
  71.         HashTable.Insert(TPEmployees[i], nil);
  72.     HashTable.debugPrint('tstHash6.LOG', true);
  73.     {empty the table}
  74.     HashTable.Empty;
  75.     HashTable.debugPrint('tstHash7.LOG', false);
  76.     {fill the table with strings}
  77.     for i := 0 to pred(ElementCount) do
  78.       HashTable.Insert(Format('String%d', [i]), nil);
  79.     HashTable.debugPrint('tstHash8.LOG', true);
  80.   finally
  81.     HashTable.Free;
  82.   end;
  83. end.
  84.